<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('admin_common', 'admin', 0);
class content_manage extends admin_common
{
	private $db;
	private $position;
	
	public function __construct()
	{
		parent::__construct();
		$this->db = lk::load_model('content');
		//$this->posdb = lk::load_model('position');
		//set_cache('position', $this->posdb->get('*', '', '', '', '', 'posid'));
		$this->position = get_cache('position');
	}
	
	/**
	 * 编辑
	 */
	public function edit()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			$info['posids'] = isset($info['posid']) && is_array($info['posid']) && $info['posid'] ? implode(',', $info['posid']) : '';
			$info['info']  = is_array($info['c']) && $info['c'] ? new_addslashes(var_export($info['c'], true)) : '';
			if($info['color'] && $info['weight'])
			{
				$info['style'] = $info['color'] . ',' . $info['weight'];
			}
			else if($info['color'] || $info['weight'])
			{
				$info['style'] = $info['color'] . $info['weight'];
			}
			else
			{
				$info['style'] = '';
			}
			$id = (int)$info['id'];
			$info['updatetime'] = TIME;
			$fields = array('title',
							'style',
							'thumb',
							'allow_comment',
							'posids',
							'content',
							'listorder',
							'updatetime', 
							'info',
							'price',
							'objecttype',
							'pxfangshi');
			$info = in_fields($info, $fields);
			if($this->db->update($info, "`id`='$id'"))
			{
				showmsg(LANG('update_success'), true, '', '', 'edit_content');
			}
			else
			{
				showmsg(LANG('update_faild'), false);
			}
		}
		else
		{
			if(!isset($_GET['id']) || !$_GET['id'])
			{
				showmsg(LANG('parameters_error'), false);
			}
			$id = (int)$_GET['id'];
			lk::lo('form', '', 0);
			$cats    = get_cache('categorys');
			$pos  = $this->position;
			$objs = $this->db->objs;
			$pxfs = $this->db->pxfs;
			$info = $this->db->get_one('*', "`id`='$id'");
			$catname = $cats[$info['catid']]['catname'];
			if(strpos($info['style'], ','))
			{
				$color = $fw = '';
				list($color, $fw) = explode(',', $info['style']);
				$info['styles'] = " color:" . $color . ";font-weight:" . $fw . ";";
			}
			else if($info['style'] == 'bold')
			{
				$info['styles'] = ' font-weight:bold;';
			}
			else if($info['style'])
			{
				$info['styles'] = ' color:' . $info['style'] . ';';
			}
			else
			{
				$info['styles'] = '';
			}
			eval("\$i=" . $info['info'] . ";");
			$man  = isset($i['man']) ? $i['man'] : '';
			$email= isset($i['email']) ? $i['email'] : '';
			$tel  = $i['tel'];
			$addr = $i['addr'];
			include self::admin_t('content_edit');
		}
	}
	
	/**
	 * 禁用
	 */
	public function disable()
	{
		if(isset($_POST['id']) && $_POST['id'])
		{
			$id = (int)$_POST['id'];
			$r = $this->db->update(array('status'=>0), "`id`='$id'");
			$r ? exit('1') : exit('0');
		}
		exit('0');
	}
	
	/**
	 * 删除
	 */
	public function delete()
	{
		if(isset($_POST['id']) && $_POST['id'])
		{
			$in = '';
			$logid_arr = explode(',', $_POST['id']);
			foreach($logid_arr as $logid)
			{
				if($logid)
				{
					$in .= '\'' . (int)$logid . '\',';
				}
			}
			$ids = substr($in, 0, -1);
			if($this->db->delete("`id` IN ($ids)"))
			{
				exit('1');
			}
		}
		exit('0');
	}
	
	/**
	 * 排序
	 */
	public function listorder()
	{
		if(isset($_POST['dosubmit']))
		{
			$listorder = $_POST['listorder'];
			foreach($listorder as $id => $order)
			{
				$id = (int)$id;
				$this->db->update(array('listorder'=>(int)$order), "`id`='$id'");
			}
			showmsg(LANG('update_success'), true);
		}
	}
	
	public function initialize()
	{
		$cats    = get_cache('categorys');
		$pos = array();
		foreach($this->position as $tp)
		{
			$pos[$tp['posid']] = $tp['name'];
		}
		$catid   = isset($_GET['catid']) ? (int)$_GET['catid'] : 1;
		$catinfo = $cats[$catid];
		$page    = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$content = $this->db->listinfo("*", "`catid`='$catid' AND `status`='1'", $page, 15, '`id` DESC');
		$page_nav= $this->db->page_nav; 
		if($content)
		{
			foreach($content as $kk => $cc)
			{
				$poss = null;
				if(strpos($cc['style'], ','))
				{
					list($color, $fw) = explode(',', $cc['style']);
					$style = " style='color:" . $color . ";font-weight:" . $fw . "'";
				}
				else if($cc['style'] == 'bold')
				{
					$style = ' style="font-weight:bold"';
				}
				else if($cc['style'])
				{
					$style = ' style="color:' . $cc['style'] . '"';
				}
				else
				{
					$style = '';
				}
				$posids = array_filter(explode(',', $cc['posids']));
				$poss = '';
				if($posids)
				{
					foreach($posids as $posid)
					{
						$poss .= $pos[$posid] . ',';
					}
				}
				$poss = substr($poss, 0, -1);
				$content[$kk]['posids'] = $poss;
				unset($poss, $posid, $posids);
				$content[$kk]['title']  = "<span$style>" . $cc['title'] . "</span>";
			}
		}
		include self::admin_t('content_manage');
	}
	
	/**
	 * 添加内容页面
	 */
	public function add_content()
	{	
		$cats    = get_cache('categorys');
		$catid   = isset($_GET['catid']) ? (int)$_GET['catid'] : 1;
		$catinfo = $cats[$catid];
		$pos     = $this->position;
		lk::lo('form', '', 0);	
		$objs  = $this->db->objs;
		$pxfs  = $this->db->pxfs;
		include self::admin_t('content_add');
	}
	
	/**
	 * 添加内容动作
	 */
	public function add()
	{
		$info = isset($_POST['dosubmit']) ? $_POST['info'] : array();
		$info['posids'] = isset($info['posid']) && is_array($info['posid']) && $info['posid'] ? implode(',', $info['posid']) : '';
		$info['info']  = is_array($info['c']) && $info['c'] ? new_addslashes(var_export($info['c'], true)) : '';

		if($info['color'] && $info['weight'])
		{
			$info['style'] = $info['color'] . ',' . $info['weight'];
		}
		else if($info['color'] || $info['weight'])
		{
			$info['style'] = $info['color'] . $info['weight'];
		}
		else
		{
			$info['style'] = '';
		}
		
		$info['status']= 1;
		$info['updatetime'] = $info['inputtime'] = strtotime($info['inputtime']);
		$info['username'] = get_cookie('username');
		$info['userid']   = $_SESSION['userid'];
		$fields = array('catid',
						'title',
						'style',
						'thumb',
						'allow_comment',
						'posids',
						'content',
						'listorder',
						'status',
						'userid',
						'username',
						'inputtime',
						'updatetime', 
						'info',
						'price',
						'objecttype',
						'pxfangshi');
		$info = in_fields($info, $fields);
		if($this->db->insert($info))
		{
			showmsg(LANG('insert_success'), true, '', '', 'add_content');
		}
		else
		{
			showmsg(LANG('insert_faild'), false);
		}
	}
	
	/**
	 * 检查标题是否存在
	 */
	public function check_title_ajax()
	{
		$title = isset($_GET['title']) && $_GET['title'] ? $_GET['title'] : exit('0');
		$this->db->get_title($title) ? exit('0') : exit('1');
	}
}